import 'dart:math';

import 'package:base_lib/common/bloc/base_bloc.dart';
import 'package:base_lib/index.dart';
import 'package:rxdart/rxdart.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:base_lib/dictionary/dictionary_provider.dart';
import 'package:base_lib/dictionary/entity/medical_section_entity.dart'
    as SectionBean;

class SelectSectionBloc extends BaseBloc {
  final PublishSubject<List<SectionBean.Data>> topSection = PublishSubject();

  final PublishSubject<List<SectionBean.Data>> childSection = PublishSubject();

  List<SectionBean.Data> parentList;
  List<SectionBean.Data> childList;

  SectionBean.Data selectedTopSection;
  SectionBean.Data selectedChildSection;

  String checkedTopCode = '';

  String checkedChildCode = '';

  int pIndex = 0;
  int cIndex = 0;

  setSectionCode(String sectionCode) {
    try {
      checkedTopCode = sectionCode.split('_')[0];
    } catch (e) {
      print(e);
      checkedTopCode = sectionCode;
    }
    checkedChildCode = sectionCode;
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    topSection?.close();
    childSection?.close();
  }

  getSection(BuildContext context) async {
    var result = await DictionaryProvider.getSection();
    if (result.isSuccess) {
      parentList = SectionBean.MedicalSectionEntity.fromJson(result.data).data;
      topSection.add(parentList);
      try {
        pIndex = parentList
            .indexWhere((element) => element.dictCode == checkedTopCode);
        var p = parentList[pIndex];
        if (p != null) {
          getSection2(context, p);
        }
      } catch (e) {
        print(e);
      }
    } else {
      ToastUtil.error(result.error.msg);
    }
  }

  getSection2(BuildContext context, SectionBean.Data p) async {
    selectedTopSection = p;
    checkedTopCode = p.dictCode;
    topSection.add(parentList);
    selectedChildSection = null;
    var result = await DictionaryProvider.getSection2(p.systemCode, p.dictCode);
    if (result.isSuccess) {
      childList?.clear();
      childList ??= [];
      childList.add(p);
      var tempChildList =
          SectionBean.MedicalSectionEntity.fromJson(result.data).data;
      childList.addAll(tempChildList);
      childSection.add(childList);

      try {
        cIndex = childList
            .indexWhere((element) => element.dictCode == checkedChildCode);
        var c = childList[cIndex];
        if (c != null) {
          setSelectedChildSection(c);
        }
      } catch (e) {
        print(e);
      }
    } else {
      ToastUtil.error(result.error.msg);
    }
  }

  setSelectedChildSection(SectionBean.Data child) {
    selectedChildSection = child;
    checkedChildCode = child.dictCode;
    childSection.add(childList);
  }

  void save(BuildContext context) {
    if (selectedTopSection == null) {
      ToastUtil.info("请选择一级科室");
      return;
    }
    if (selectedChildSection == null) {
      ToastUtil.info("请选择二级科室");
      return;
    }

    Routers.pop(context, selectedChildSection);
  }
}
